"ui";

// -------------------------------------
function addition(key,value){
    if (key,value){
    var url = "http://jp-tyo-ilj-2.natfrp.cloud:12779/addition?key="+key+"&value=" + value;
    res = http.get(url);
    toast(res.body.string());
    }
    else if (key==null){
        toast("您取消了打卡");
    }
}

function view(){
    "ui";
    var res = http.get(url = "http://jp-tyo-ilj-2.natfrp.cloud:12779/view");
    let result = res.body.json();
    for(var key in result){

        var msg = msg + key +":" +  result[key] + "\n";
    }
    var items = msg.slice(9,msg.length);
    alert("运动数据",items);
}
function main(){
    let options = ["哑铃拉伸", "俯卧撑", "杠铃拉伸"]
    let i = dialogs.select("请选择一个选项", options);
    if(i!=-1){
        toast("您选择了:" + options[i]);
        let value = parseInt(rawInput("请输入打卡数量:"));
        addition(i+1,value);
        return true;
    }
    else{
        toast("您取消了选择");
        return false;
    }
}



ui.layout(
    <frame  w="*" h="*" bg="#000000">
        <img src="https://pic1.zhimg.com/80/v2-d5011c32ec651f7b3c63c26a69801e09_720w.jpg?source=1940ef5c" scaleType="fitXY"/>
  
        <button id="ok" layout_gravity="center" w="auto" h="auto" text="查看数据" textSize="30sp"/>
        <button id="add" layout_gravity="center" marginTop="80" w="auto" h="auto" text="运动打卡" textSize="30sp"/>


    </frame>
);
ui.ok.click(function(){
    console.log("我被点击了");
    
    thread = threads.start(function(){
        //在新线程执行的代码
        view();
        thread.interrupt();
    });
    
});

ui.add.click(function(){
    console.log("我被点击了");
    
    thread = threads.start(function(){
        //在新线程执行的代码
        main();
    });
    
});
